import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-04-12
 * Time: 12:21
 */
public class CAS {
    public static void main(String[] args) throws InterruptedException {
        // 创建原子类，初始化值为0
        AtomicInteger num = new AtomicInteger(0);

        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                // 此操作相当于num++
                num.getAndIncrement();
                // 此操作相当于++num
                //num.incrementAndGet();
                // 此操作相当于num--
                //num.getAndDecrement();
                // 此操作相当于--num
                //num.decrementAndGet();
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                num.getAndIncrement();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(num);

    }
}
